package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.config.Calibration;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 保存setmeal
     * @param setmeal
     */
    @Override
    public void saveSetmeal(Setmeal setmeal) {
        //1.校验数据
        Calibration.Setmeal(setmeal);
        //2.保存数据
        setmealMapper.saveSetmeal(setmeal);
        log.info("保存setmeal：{}", setmeal);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询，参数：{}", setmealPageQueryDTO);
        //1,校验参数
        Calibration.pageQuery(setmealPageQueryDTO);
        //2,使用分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //3,查询数据
        Page<SetmealVO> page =setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void update(Setmeal setmeal) {
        log.info("修改setmeal：{}", setmeal);
        //1.校验数据
        Calibration.Setmeal(setmeal);
        //2.修改数据
        setmealMapper.update(setmeal);
    }

    @Override
    public Setmeal getById(Long id) {
        log.info("根据id查询套餐：{}", id);
        //1.校验参数
        if (Objects.isNull(id)){
            throw new RuntimeException("参数异常为空");
        }
        //2.调用服务层
       Setmeal setmeal = setmealMapper.getById(id);
        return setmeal;
    }

    /**
     * 启售停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //1.校验参数
        if (Objects.isNull(id)){
            throw new RuntimeException("参数异常为空");
        }
        if (Objects.isNull(status)){
            throw new RuntimeException("参数异常为空");
        }
        //2.调用m层
        Setmeal build = new Setmeal().builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(build);

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        //1.校验参数
        if (Objects.isNull(ids)){
            throw new RuntimeException("参数异常为空");
        }
        //2.调用m层
        for (Long id : ids) {
            setmealMapper.deleteById(id);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
